/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * spelPanel.java
 *
 * Created on 13-jun-2010, 22:41:48
 */
package View;

import cradleofrome.*;
import java.awt.*;

/**
 *
 * @author Tim
 */
public class SpelPanel extends javax.swing.JPanel {

    /** Creates new form spelPanel */
    private Spelveld spelveld1;

    public SpelPanel(Spelveld spelveld) {
        
        spelveld1 = spelveld;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setPreferredSize(new java.awt.Dimension(250, 250));
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
    }//GEN-LAST:event_formKeyPressed
    /*
     * main teken methode
     * @param Graphics g
     */

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        //teken het grid
        drawGrid(g);

        //teken de selector
        drawSelector(g);
    
        //teken de spelobjecten
        drawSpelObjects(g);
    }

    /*
     * teken een raster met vakjes van 25*25 pixels
     * @param Graphics g
     */
    private void drawGrid(Graphics g) {

        g.setColor(Color.black);
        int x = 25;
        int y = 25;
        int horSize = spelveld1.getVeldGrootX() * 25;
        int verSize = spelveld1.getVeldGrootY() * 25;
        while (x <= horSize) {
            g.drawLine(x, 0, x, verSize);
            x += 25;
        }

        while (y <= verSize) {
            g.drawLine(0, y, horSize, y);
            y += 25;
        }
    }

    /*
     * teken de selector als een groene cirkel met straal 25 pixels
     * @param Graphics g
     */
    private void drawSelector(Graphics g) {

        Selector selektor = spelveld1.getSelector();
        Positie mijnpositie = selektor.getPositie();
        int x = mijnpositie.getX() * 25;
        int y = mijnpositie.getY() * 25;
        g.setColor(Color.green);
        g.fillOval(x, y, 25, 25);
    }

    /*
     * teken alle overige spelobjecten als vierkanten met kleuren blauw en rood
     * @param Graphics g
     */
    private void drawSpelObjects(Graphics g) {

        for(int y_count = 0 ; y_count < spelveld1.getVeldGrootY(); y_count++){
            for(int x_count = 0 ; x_count < spelveld1.getVeldGrootX(); x_count++){
        
                Positie positie = spelveld1.getPositie(x_count, y_count);
                if(positie.getSpelobject() != null && positie.getSpelobject().isBeweegBaar() == true){

                    int x = positie.getX() * 25;
                    int y = positie.getY() * 25;
                    g.setColor(Color.BLUE);
                    g.fillRect(x, y, 25, 25);
                }
            else if(positie.getSpelobject() != null && positie.getSpelobject().isBeweegBaar() == false){

                 int x = positie.getX() * 25;
                 int y = positie.getY() * 25;
                 g.setColor(Color.red);
                 g.fillRect(x, y, 25, 25);
        }
        }

    

    }
        }
}

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

